/* GUI CONVERTED from rqrtmt0.p (converter v1.76) Tue Mar 19 08:25:17 2002 */
/* rqrtmt0.p - REQUISITION REROUTE                                            */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.25 $                                                          */
/*                                                                            */
/*V8:ConvertMode=Maintenance                                                  */
/* This is a subprogram to handle the re-routing of a requisition based on    */
/* out of tollerance limits                                                   */
/*                                                                            */
/* REVISION: 8.5    LAST MODIFIED: 05/05/97       BY: *J1Q2* B. Gates         */
/* REVISION: 8.5    LAST MODIFIED: 10/22/97       BY: *J243* B. Gates         */
/* REVISION: 8.6E   LAST MODIFIED: 02/23/98       BY: *L007* A. Rahane        */
/* REVISION: 8.5    LAST MODIFIED: 03/13/98       BY: *J2GM* B. Gates         */
/* REVISION: 8.6E   LAST MODIFIED: 10/04/98       BY: *J314* Alfred Tan       */
/* REVISION: 8.6E   LAST MODIFIED: 01/13/99       BY: *J38J* Steve Nugent     */
/* REVISION: 9.1    LAST MODIFIED: 03/24/00       BY: *N08T* Annasaheb Rahane */
/* Revision: 1.17     BY: Bill Pedersen           DATE: 04/25/00  ECO: *N059* */
/* Revision: 1.18     BY: Hualin Zhong            DATE: 08/15/00  ECO: *N0LC* */
/* Revision: 1.19     BY: Zheng Huang             DATE: 10/12/00  ECO: *N0S0* */
/* Revision: 1.20     BY: Sandeep P.              DATE: 05/22/01  ECO: *N0Z0* */
/* Revision: 1.21     BY: Jean Miller             DATE: 03/04/02  ECO: *N1BS* */
/* $Revision: 1.25 $  BY: Jean Miller             DATE: 03/06/02  ECO: *N1BW* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}
{pxmaint.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rqrtmt0_p_1 "Maintain Aprvl Comments"
/* MaxLen: Comment: */

&SCOPED-DEFINE rqrtmt0_p_2 "Currently Routed To"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

/* REQUISITION SYSTEM GLOBAL CONSTANTS */
{rqconst.i}

/* NEW SHARED VARIABLES */
define new shared variable approver    like rqa_apr no-undo.
define new shared variable action_type like action_entry.
define new shared variable cmtindx     as integer.

/* LOCAL VARIABLES */
define variable all_info_correct      like mfc_logical no-undo.
define variable approval_comments     like mfc_logical label {&rqrtmt0_p_1} no-undo.
define variable route_reqnbr          like rqm_mstr.rqm_nbr.
define variable route_to_apr          like rqm_rtto_userid no-undo.
define variable route_to_buyer        like po_buyer        no-undo.
define variable route_to_purchasing   like mfc_logical     no-undo.
define variable route_to_userid       like rqm_rtto_userid no-undo.
define variable current_route_to      as character label {&rqrtmt0_p_2}.
define variable date_time             as character no-undo.
define variable email_sent_to         as character no-undo.
define variable messages              as character no-undo.
define variable msglevels             as character no-undo.
define variable reverse               as logical   no-undo.
define variable alternate_ok          as logical   no-undo.
define variable approvals_completed   as logical   no-undo.
define variable update_initial_fields as logical   no-undo initial true.
define variable continue              as logical   no-undo.

define variable exit_program as logical   no-undo initial true.

define variable l_rqm_recno as recid no-undo.
define variable l_action_entry_lbl as character format "x(30)" no-undo.
define variable other-browse       as character no-undo.

/* APPROVER LIST TEMPTABLE DEFINITIONS */
{rqalttd.i new}


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
action_entry         colon 30 format "x(1)"
   l_action_entry_lbl   no-label
   current_route_to     colon 30
   usr_name             no-labels
   route_reqnbr         colon 30
   skip(1)
   approval_comments    colon 30
   route_to_apr         colon 30
   route_to_buyer       colon 50
 SKIP(.4)  /*GUI*/
with frame a attr-space side-labels width 80 NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

l_action_entry_lbl:screen-value in frame a =
   "1=" + getTermLabel("ROUTE_TO",12) +
   " 2=" + getTermLabel("REVERSE_ROUTE_TO",21).

/* SET THE INDICATOR THAT CONTROLS IF INITIAL FIELDS ARE UPDATED.      */
/* THESE FIELDS ARE UPDATED ONLY WHEN THIS PROGRAM IS INVOKED          */
/* FROM THE MENU DRIVER PROGRAM RQRTMT.P.  OTHERWISE THEY              */
/* JUST DEFAULT.  IF THE EDIT CHECKS ON THE INITIAL UPDATE DON'T PASS, */
/* THEN THIS FLAG IS TURNED ON AND WE GO UPDATE THE FIELDS.            */

update_initial_fields = index(program-name(2),"rqrtmt.") > 0.
exit_program = index(program-name(2),"popomtrc.") > 0.

action_entry = "1".
display action_entry approval_comments with frame a.

/* GET MOST RECENTLY ACCESSED REQ NUMBER AND DISPLAY */

{gprun.i ""rqidf.p""
   "(input 'get',
     input 'reqnbr',
     input-output route_reqnbr)"}
/*GUI*/ if global-beam-me-up then undo, leave.


display route_reqnbr with frame a.

/* GET MOST RECENTLY ACCESSED APPROVER AND DISPLAY IN CURRENT ROUTE-TO */
{gprun.i ""rqidf.p""
   "(input 'get',
     input 'approver',
     input-output current_route_to)"}
/*GUI*/ if global-beam-me-up then undo, leave.


if current_route_to = "" then current_route_to = global_userid.

display current_route_to with frame a.

mainloop:
repeat on endkey undo, leave:
/*GUI*/ if global-beam-me-up then undo, leave.

   route_to_purchasing = false.

   do on error undo, retry on endkey
      undo mainloop, leave mainloop:
/*GUI*/ if global-beam-me-up then undo, leave.

      if update_initial_fields then do:
         set action_entry with frame a.
      end.

      action_type = "2" + action_entry.

      if action_type <> ACTION_ROUTE
         and action_type <> ACTION_ROUTE_REVERSE
      then do:
         /* MESSAGE #2052 - INVALID ACTION */
         {pxmsg.i &MSGNUM=2052 &ERRORLEVEL={&APP-ERROR-RESULT}}
         update_initial_fields = true.
         undo, retry.
      end.

      /* Determine which lookup to run */
      if action_type = ACTION_ROUTE then
         other-browse = "".
      else
         other-browse = "gplu531.p".
      {gprun.i ""gpbranch.p""
         "(input 'rqlu012.p', input other-browse)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   end.
/*GUI*/ if global-beam-me-up then undo, leave.


   do on error undo, retry
      on endkey undo mainloop, retry mainloop:
/*GUI*/ if global-beam-me-up then undo, leave.

      if update_initial_fields then do:
         set current_route_to
         with frame a
            editing:
            {mfnp.i usr_mstr
               current_route_to usr_userid current_route_to usr_userid
               usr_userid}

            if recno <> ? then do:
               display usr_userid @ current_route_to usr_name
               with frame a.
            end.
         end.
      end.

      {pxrun.i &PROC='processRead'
               &PROGRAM='gpurxr.p'
               &PARAM="(input current_route_to,
                        buffer usr_mstr,
                        input no,
                        input no)"
               &NOAPPERROR=true &CATCHERROR=true}

      if return-value <> {&SUCCESS-RESULT} then do:
         /* MESSAGE #5605 - USER ID DOES NOT EXIST */
         {pxmsg.i &MSGNUM=5605 &ERRORLEVEL={&APP-ERROR-RESULT}}
         update_initial_fields = true.
         undo, retry.
      end.

      display usr_name with frame a.
      approver = current_route_to.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.


   /* PASS CURRENT ROUTE-TO VALUE TO LOOKUP BROWSE */
   {gpbrparm.i &browse=rqlu012.p &parm=c-brparm1 &val=current_route_to}

   do on error undo, retry
      on endkey undo mainloop, retry mainloop:
/*GUI*/ if global-beam-me-up then undo, leave.


      if update_initial_fields then do:
         set route_reqnbr
         with frame a
            editing:
            if action_type = ACTION_ROUTE_REVERSE then do:
               /* LOOK AT ALL REQS */
               {mfnp05.i rqm_mstr
                         rqm_nbr
                         "yes"
                         rqm_nbr
                         "input frame a route_reqnbr"}
            end.
            else do:
               /* LOOK AT REQS ROUTED TO THIS APPR */
               {mfnp05.i rqm_mstr
                         rqm_rtto
                         "rqm_rtto_userid = current_route_to and rqm_open"
                         rqm_nbr
                         "input frame a route_reqnbr"}
            end.

            if recno <> ? then do:
               display rqm_nbr @ route_reqnbr with frame a.
            end.
         end.
      end.

      /* ENSURE THE REQUISITION EXISTS */
      {pxrun.i &PROC='processread'
               &PROGRAM='rqgrsxr.p'
               &PARAM="(input route_reqnbr,
                        buffer rqm_mstr,
                        input {&LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"
               &NOAPPERROR=true
               &CATCHERROR=true}

      l_rqm_recno = recid(rqm_mstr).

      if return-value <> {&SUCCESS-RESULT} then do:
         /*REQUISITION DOES NOT EXIST*/
         {pxmsg.i &MSGNUM=193 &ERRORLEVEL={&APP-ERROR-RESULT}}
         update_initial_fields = true.
         undo, retry.
      end.

      /*ENSURE REQ IS OPEN*/
      {pxrun.i &PROC='validateReqIsOpen'
               &PROGRAM='rqgrsxr.p'
               &PARAM="(input rqm_nbr)"
               &NOAPPERROR=true
               &CATCHERROR=true}

      if return-value <> {&SUCCESS-RESULT} then do:
         update_initial_fields = true.
         undo, retry.
      end.

      /* ENSURE THAT FOR THE ACTION SELECTED AND WHAT THE USER
         ENTERS IN "CURRENTLY ROUTED TO", IS VALID FOR THE REQ SELECTED*/
      {pxrun.i &PROC='validateCurrentlyRoutedTo'
               &PROGRAM='rqgrsxr.p'
               &PARAM="(input rqm_nbr,
                        input global_userid,
                        input action_type,
                        input current_route_to)"
               &NOAPPERROR=true
               &CATCHERROR=true}

      if return-value <> {&SUCCESS-RESULT} then do:
         if exit_program then return error.
         update_initial_fields = true.
         undo, retry.
      end.

      /* BUILD APPROVER LIST.  THIS INCLUDES THE TEMPTABLE "ttcal" WHICH
         IS USED BY THE LOOKUP PROGRAM (SWAL.P) FOR THE FIELD route_to_apr.*/
      {gprun.i ""rqbal.p"" "(input recid(rqm_mstr))"}
/*GUI*/ if global-beam-me-up then undo, leave.


      if action_type = ACTION_ROUTE then do:
         {pxrun.i &PROC='validateAlternateApprover'
                  &PROGRAM='rqgrsxr.p'
                  &PARAM="(input rqm_nbr,
                           input current_route_to,
                           input global_userid)"
                  &NOAPPERROR=true &CATCHERROR=true}

         if return-value <> {&SUCCESS-RESULT} then do:
            undo, retry.
         end.
      end.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.


   if not available rqm_mstr
      and l_rqm_recno <> ?
   then
      find rqm_mstr
         where recid(rqm_mstr) = l_rqm_recno
      exclusive-lock no-error.

   l_rqm_recno = ?.

   /* ENTER APPROVAL COMMENTS IF REQUESTED */
   set approval_comments with frame a.

   if approval_comments then do:
      assign
         global_ref = rqm_vend
         cmtindx = rqm_apr_cmtindx.

      {gprun.i ""gpcmmt01.p"" "(input ""rqm_mstr"")"}
/*GUI*/ if global-beam-me-up then undo, leave.

      rqm_apr_cmtindx = cmtindx.
   end.

   /*GET MOST RECENTLY SAVED ROUTE-TO AND DISPLAY.  THIS COMES ONLY
   FROM PO MAINT (WHEN AN OUT OF TOLERANCE REQ IS ROUTED BACK).*/
   {gprun.i ""rqidf.p""
      "(input 'get', input 'route to', input-output route_to_apr)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   route_to_buyer = rqm_buyer.

   display
      route_to_apr
      route_to_buyer
   with frame a.

   if action_type = ACTION_ROUTE then do:
      /* UPDATE THE REQUISITION APPROVAL STATUS */
      /* CHECK FOR FINAL APPROVAL               */
      {gprun.i ""rqcfa.p""
         "(input rqm_nbr, output approvals_completed)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      /*IF FULLY APPROVED ASK WHERE TO ROUTE IT TO (APPROVER OR BUYER)*/
      if not rqm_rtdto_purch
         and approvals_completed
      then do:
         route_to_purchasing = true.

         /*APPROVALS HAVE BEEN COMPLETED, ROUTE TO PURCHASING?*/
         {pxmsg.i
            &MSGNUM=2075
            &ERRORLEVEL={&INFORMATION-RESULT}
                  &CONFIRM=route_to_purchasing}
      end.

      if rqm_rtdto_purch then do:
         route_to_purchasing = false.

         /*ROUTE TO ANOTHER BUYER?*/
         {pxmsg.i
            &MSGNUM=2232
            &ERRORLEVEL={&INFORMATION-RESULT}
                  &CONFIRM=route_to_purchasing}
      end.

      /* GET ROUTE TO AND BUYER */
      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

         set
            route_to_apr when (not route_to_purchasing)
            route_to_buyer
            with frame a
         editing:
            if frame-field = "route_to_apr" then do:
               {mfnp.i ttcal route_to_apr
                       ttcal_apr_userid route_to_apr
                       ttcal_apr_userid ttcal_apr}

               if recno <> ? then do:
                  display ttcal_apr_userid @ route_to_apr
                  with frame a.
               end.
            end.
            else
               if frame-field = "route_to_buyer" then do:
               {mfnp05.i
                  rqr_mstr
                  rqr_role
                  "rqr_role = BUYER"
                  rqr_userid
                  "input frame a route_to_buyer"}

               if recno <> ? then do:
                  display rqr_userid @ route_to_buyer
                  with frame a.
               end.
            end.
         end.

         /*VALIDATE THE ROUTE-TO APPROVER*/
         if not route_to_purchasing then do:
            {pxrun.i &PROC='validateRouteToUser'
                     &PROGRAM='rqgrsxr.p'
                     &PARAM="(input rqm_nbr, input route_to_apr)"
                     &NOAPPERROR=true
                     &CATCHERROR=true}

            if return-value <> {&SUCCESS-RESULT} and
               return-value <> {&WARNING-RESULT} then do:
               next-prompt route_to_apr with frame a.
               undo, retry.
            end.
         end.

         /*VALIDATE THE ROUTE-TO BUYER*/
         if route_to_buyer > "" or route_to_purchasing then do:
            {pxrun.i &PROC='validateBuyer'
                     &PROGRAM='rqgrsxr.p'
                     &PARAM="(input route_to_buyer)"
                     &NOAPPERROR=true
                     &CATCHERROR=true}

            if return-value <> {&SUCCESS-RESULT} then do:
               next-prompt route_to_buyer with frame a.
               undo, retry.
            end.
         end.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.

   end.

   all_info_correct = true.
   /*IS ALL INFORMATION CORRECT? */
   {pxmsg.i &MSGNUM=12
            &ERRORLEVEL={&INFORMATION-RESULT}
            &CONFIRM=all_info_correct}

   if not all_info_correct then undo, retry.

   /* UPDATE REQUISITION */

   if action_type = ACTION_ROUTE then do: /* ACTION = 1 (ROUTE TO) */
      /* IF ALL APPROVERS HAVE APPROVED AND THE USER SAID OK
         TO ROUTE TO PURCHASING, THEN MARK THE REQ APPROVED*/

      if not rqm_rtdto_purch and route_to_purchasing then do:
         {pxrun.i &PROC='setRequisitionStatusToApproved'
                  &PROGRAM='rqgrsxr.p'
                  &PARAM="(input rqm_nbr)"
                  &NOAPPERROR=true
                  &CATCHERROR=true}
      end.

      /*RECORD THE ROUTE-TO (APPROVER OR BUYER) IN THE REQ*/
      route_to_userid = if route_to_purchasing then
                           route_to_buyer
                        else route_to_apr.

      {pxrun.i &PROC='routeRequisitionTo'
               &PROGRAM='rqgrsxr.p'
               &PARAM="(input rqm_nbr, input route_to_purchasing,
                        input global_userid,  input route_to_userid)"
               &NOAPPERROR=true
               &CATCHERROR=true}
   end.
   else do: /* ACTION = 2 (REVERSE ROUTE TO) */
      {pxrun.i &PROC='reverseRouteRequisitionTo'
               &PROGRAM='rqgrsxr.p'
               &PARAM="(input rqm_nbr)"
               &NOAPPERROR=true
               &CATCHERROR=true}
   end.

   /*ASSIGN BUYER*/
   {pxrun.i &PROC='setRequisitionBuyer'
            &PROGRAM='rqgrsxr.p'
            &PARAM="(input rqm_nbr,
                     input route_to_buyer)"
            &NOAPPERROR=true
            &CATCHERROR=true}

   /*SEND EMAILS*/
   {gprun.i ""rqemsend.p""
      "(input recid(rqm_mstr),
        input action_type,
        output email_sent_to)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   /*WRITE HISTORY RECORD*/

   {gprun.i ""rqwrthst.p""
      "(input rqm_nbr,
        input 0,
        input action_type,
        input current_route_to,
        input rqm_rtto_userid,
        input email_sent_to)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   /* BLANK OUT ANY SAVED ROUTE-TO */

   route_to_apr = "".

   {gprun.i ""rqidf.p""
      "(input 'put',
        input 'route to',
        input-output route_to_apr)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   if not update_initial_fields then leave.
end.
/*GUI*/ if global-beam-me-up then undo, leave.


hide frame a no-pause.
